package com.example.dianchiwater;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URLEncoder;

public class MainActivity extends AppCompatActivity {
    //初始化
    Button forget,zhuce,login;
    EditText LoginID,LoginPassword;

    String Phone,Password;


    private final int LOGINSUCCESS=0;
    private final int LOGINNOTFOUND=1;
    private final int LOGINEXCEPT=2;
    private final int REGISTERSUCCESS=3;
    private final int REGISTERNOTFOUND=4;
    private final int REGISTEREXCEPT=5;

    @SuppressLint("HandlerLeak")
    Handler handler=new Handler(){                                      //消息机制，用来在子线程中更新UI
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){                                          //具体消息，具体显示，回传数据
                case LOGINSUCCESS:
                    Toast.makeText(getApplicationContext(),(String)msg.obj,Toast.LENGTH_LONG).show();
                    break;
                case LOGINNOTFOUND:
                    Toast.makeText(getApplicationContext(),(String)msg.obj,Toast.LENGTH_LONG).show();
                    break;
                case LOGINEXCEPT:
                    Toast.makeText(getApplicationContext(),(String)msg.obj,Toast.LENGTH_LONG).show();
                    break;
                case REGISTERSUCCESS:
                    Toast.makeText(getApplicationContext(),(String)msg.obj,Toast.LENGTH_LONG).show();
                    break;
                case REGISTERNOTFOUND:
                    Toast.makeText(getApplicationContext(),(String)msg.obj,Toast.LENGTH_LONG).show();
                    break;
                case REGISTEREXCEPT:
                    Toast.makeText(getApplicationContext(),(String)msg.obj,Toast.LENGTH_LONG).show();
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //寻址
        forget = findViewById(R.id.Forget);//忘记密码
        login = findViewById(R.id.Login);//登录
        zhuce = findViewById(R.id.Zhuce);//注册

        LoginPassword = findViewById(R.id.loginPassword);//登录密码
        LoginID = findViewById(R.id.loginID);//登录账号

        zhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this,Zhuce.class);
                startActivity(i);
            }
        });
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Phone = LoginID.getText().toString().trim();
                Password = LoginPassword.getText().toString().trim();
                Intent i = new Intent(MainActivity.this,YinDaoYe.class);
                startActivity(i);
                finish();
                if(LoginID.equals("")|LoginPassword.equals("")){                 //判断是否为空
                    Toast.makeText(MainActivity.this,"用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                }
                else {
                    //操作子线程
                    new Thread(){

                        private HttpURLConnection connection;


                        @Override
                        public void run() {

                            try {
                                //封装成传输数据的键值对,无论get还是post,传输中文时都要进行url编码（RULEncoder）
                                // 如果是在浏览器端的话，它会自动进行帮我们转码，不用我们进行手动设置
                                String data2= "username="+ URLEncoder.encode(Phone,"utf-8")+"&password="+ URLEncoder.encode(Password,"utf-8")+"&sign="+URLEncoder.encode("1","utf-8");
                                connection=HttpConnectionUtils.getConnection(data2);
                                int code = connection.getResponseCode();
                                if(code==200){
                                    InputStream inputStream = connection.getInputStream();
                                    String str = StreamChangeStrUtils.toChange(inputStream);//写个工具类流转换成字符串
                                    Message message = Message.obtain();//更新UI就要向消息机制发送消息
                                    message.what=LOGINSUCCESS;//用来标志是哪个消息
                                    message.obj=str;//消息主体
                                    handler.sendMessage(message);

                                }
                                else {
                                    Message message = Message.obtain();
                                    message.what=LOGINNOTFOUND;
                                    message.obj="注册异常...请稍后再试";
                                    handler.sendMessage(message);
                                }
                            } catch (Exception e) {//会抛出很多个异常，这里抓一个大的异常
                                e.printStackTrace();
                                Message message = Message.obtain();
                                message.what=LOGINEXCEPT;
                                message.obj="服务器异常...请稍后再试";
                                handler.sendMessage(message);
                            }
                        }
                    }.start();//不要忘记开线程

                   
                }
            }
        });
    }
}